﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using eRechnung.Properties;
using ebDoc = eRechnung.Globals;


namespace eRechnung
{
    public partial class FrmHandyEinstellungen : Form
    {
        //        private const string MailBodyDefault = @"Sehr geehrte(r) Kunde, 
        //
        //beigefügt erhalten Sie die Rechnung Nr. [RECHNUNGSNR] vom [RECHNUNGSDATUM] mit dem Ersuchen um termingerechte Erledigung.
        //
        //Mit freundlichen Grüßen
        //
        //[RECHNUNGSSTELLER]
        //Kontakt: [KONTAKT]
        //Telefon: [TELEFON]
        //E-Mail: [EMAIL]";
        //        private const string BetreffDefault = @"Rechnung Nr. [RECHNUNGSNR] vom [RECHNUNGSDATUM] ";

        private Dictionary<string, InvoiceXML.SignTypes> signList = new Dictionary<string, InvoiceXML.SignTypes>()
            {
                {"Keine Signatur",InvoiceXML.SignTypes.NoSignature},
                {"Handysignatur",InvoiceXML.SignTypes.Handy},
                {"Wahlweise",InvoiceXML.SignTypes.AskAlways}
            };

        public FrmHandyEinstellungen()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

            tbxHandyNr.Text = Settings.Default.setHandyNr;
            if (Settings.Default.setVorwahl != "")
            {
                tbxHandyNr.Text = Settings.Default.setVorwahl + tbxHandyNr.Text;
            }
            tbxBetreff.Text = Settings.Default.setBetreff;
            if (tbxBetreff.Text == "")
            {
                tbxBetreff.Text = Settings.Default.defaultMailSubject;
            }
            rTbxMailBody.Text = Settings.Default.setMailText;
            if (rTbxMailBody.Text == "")
            {
                rTbxMailBody.Text = Settings.Default.defaultMailBody;
            }

            tbxEtreseorMail.Text = Settings.Default.setEtresorMail;
            cbxSendEtresor.Checked = Settings.Default.setSendEtresor;
            cbxSaveLocal.Checked = Settings.Default.setSaveLocal;
            tbxLocalFile.Text = Settings.Default.setLocalPathSigned;
            cbxSignType.Items.Clear();
            foreach (KeyValuePair<string, InvoiceXML.SignTypes> pair in signList)
            {
                var item = cbxSignType.Items.Add(pair.Key);
                if (pair.Value == (InvoiceXML.SignTypes)Settings.Default.setSignType)
                {
                    cbxSignType.SelectedIndex = item;
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnSpeichern_Click(object sender, EventArgs e)
        {
            InvoiceXML.SignTypes stype;
            string selSign = (string)cbxSignType.SelectedItem;

            stype = GetSignType(selSign);
            Settings.Default.setSignType = (int)stype;

            if (stype != InvoiceXML.SignTypes.NoSignature)
            {
                if (tbxHandyNr.Text == "")
                {
                    MessageBox.Show("Handy Nr. nicht angegeben.");
                    return;
                }
            }
            if (tbxBetreff.Text == "")
            {
                MessageBox.Show("Betreff für E-Mail Versand nicht angegeben");
                return;
            }
            if (gBxEtresor.Visible == false)
            {
                // Wenn e-tresor nicht sichtbar, dann wird auch nicht dorthin verschickt.
                cbxSendEtresor.Checked = false;
            }
            if ((cbxSendEtresor.Checked == true) && (tbxEtreseorMail.Text == ""))
            {
                MessageBox.Show("e-tresor Postfach nicht angegeben.");
                return;
            }
            if ((cbxSaveLocal.Checked == true) && (tbxLocalFile.Text == ""))
            {
                MessageBox.Show("Verzeichnis für lokales Speichern nicht angegeben.");
                return;
            }

            /* string selVorwahl = cBxVorwahl.SelectedItem.ToString(); */
            /* Settings.Default.setVorwahl = selVorwahl; */
            Settings.Default.setHandyNr = tbxHandyNr.Text;
            Settings.Default.setBetreff = tbxBetreff.Text;
            Settings.Default.setMailText = rTbxMailBody.Text;
#if !NO_ETRESOR
            Settings.Default.setSendEtresor = cbxSendEtresor.Checked;
#endif
#if NO_ETRESOR
            Settings.Default.setSendEtresor = false;
#endif
            Settings.Default.setEtresorMail = tbxEtreseorMail.Text;
            Settings.Default.setLocalPathSigned = tbxLocalFile.Text;
            Settings.Default.setSaveLocal = cbxSaveLocal.Checked;
            ebDoc.Ribbons.ebI_Ribbon.btnSignAndMail.Enabled = false; // enable sign & Mail
            ebDoc.Ribbons.ebI_Ribbon.btnMailAndSave.Enabled = false;

            switch (stype)
            {
                case InvoiceXML.SignTypes.NoSignature:
                    ebDoc.Ribbons.ebI_Ribbon.btnSignAndMail.Enabled = false; // enable sign & Mail
                    ebDoc.Ribbons.ebI_Ribbon.btnMailAndSave.Enabled = true;
                    break;
                case InvoiceXML.SignTypes.Handy:
                    ebDoc.Ribbons.ebI_Ribbon.btnSignAndMail.Enabled = true; // enable sign & Mail
                    ebDoc.Ribbons.ebI_Ribbon.btnMailAndSave.Enabled = false;
                    break;
                case InvoiceXML.SignTypes.AskAlways:
                    ebDoc.Ribbons.ebI_Ribbon.btnSignAndMail.Enabled = true; // enable sign & Mail
                    ebDoc.Ribbons.ebI_Ribbon.btnMailAndSave.Enabled = true;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            if (stype != InvoiceXML.SignTypes.NoSignature)
            {
            }

            Settings.Default.Save();
            Close();
        }

        private InvoiceXML.SignTypes GetSignType(string selSign)
        {
            InvoiceXML.SignTypes stype;
            var res = signList.TryGetValue(selSign, out stype);
            if (!res)
            {
                stype = InvoiceXML.SignTypes.NoSignature;
            }
            return stype;
        }

        private void btnGetFolder_Click(object sender, EventArgs e)
        {
            fBDInvFolder.SelectedPath = tbxLocalFile.Text;
            DialogResult rc = fBDInvFolder.ShowDialog();
            if (rc != DialogResult.OK)
            {
                return;
            }
            tbxLocalFile.Text = fBDInvFolder.SelectedPath;
            return;
        }

        private void btnResetBetreff_Click(object sender, EventArgs e)
        {
            tbxBetreff.Text = Settings.Default.defaultMailSubject;
        }

        private void btnLoadDefaultText_Click(object sender, EventArgs e)
        {
            rTbxMailBody.Text = Settings.Default.defaultMailBody;
        }

        private void tbxEtreseorMail_Validating(object sender, CancelEventArgs e)
        {
            if (cbxSendEtresor.Checked == true)
            {
                string email = tbxEtreseorMail.Text + "@e-tresor.at";
                if (ebDoc.ThisDocument.TestEmailRegex(email) != true)
                {
                    MessageBox.Show("e-tresor: Ungültige E-Mail Adresse");
                    e.Cancel = true;
                    return;
                }
            }
            e.Cancel = false;
            return;
        }

        private void cbxSignType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cbx = (ComboBox) sender;
            if (GetSignType((string)cbx.SelectedItem) == InvoiceXML.SignTypes.NoSignature)
            {
                SetSpeicherortAktiv(false);
            }
            else
            {
                SetSpeicherortAktiv(true);
            }
        }

        private void SetSpeicherortAktiv(bool isActive)
        {
            tbxHandyNr.Enabled = isActive;
            cbxSaveLocal.Enabled = isActive;
            tbxLocalFile.Enabled = isActive;
        }
    }
}
